﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using testws;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    testws.Service ws = new Service();
    private SqlConnection con;
    private SqlDataAdapter dap;
    private DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection("server=.;database=Test;uid=sa;pwd=");
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //GridView1.DataSource = ws.SearchItems(txtSearch.Text);
        //GridView1.DataBind();
        LoadData();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddNew.aspx");
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int code = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        ws.DeleteItem(code);
        LoadData();
    }

    private void LoadData()
    {
        GridView1.DataSource = ws.SearchItems(txtSearch.Text);
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int code = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string name = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        float price = float.Parse((GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text);
        string des = (GridView1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text;
        ws.UpdateItem(code, name, price, des);
        GridView1.EditIndex = -1;
        LoadData();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadData();
    }
}